<template>
    <el-card class="box-card">
        <div slot="header" class="clearfix">
            <span>基本资料</span>
        </div>
        <!-- 表单 -->
        <el-form :model="userForm" :rules="userFormRules" ref="userFormRef" label-width="100px">
            <el-form-item label="登录名称" prop="username">
                <el-input v-model="userForm.username" disabled></el-input>
            </el-form-item>
            <el-form-item label="用户昵称" prop="nickname">
                <el-input v-model="userForm.nickname" minlength="1" maxlength="10"></el-input>
            </el-form-item>
            <el-form-item label="用户邮箱" prop="email">
                <el-input v-model="userForm.email"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="hSubmit">提交修改</el-button>
                <el-button @click="resetForm">重置</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</template>
  
<script>
import { mapState } from 'vuex';
import { eDitUserinfo } from '@/api/user.js'

export default {
    name: 'UserInfo',
    data() {
        return {
            // 存储表单的数据对象
            // userForm: this.userinfo,
            // userForm: this.$store.state.user.userinfo,
            userForm: { ...this.userinfo },

            // 存储表单验证规则对象
            userFormRules: {
                nickname: [
                    { required: true, message: '请输入用户昵称', trigger: 'blur' },
                    { pattern: /^\S{1,10}$/, message: '昵称必须是1-10位的非空字符串', trigger: 'blur' }
                ],
                email: [
                    { required: true, message: '请输入用户邮箱', trigger: 'blur' },
                    { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
                ]
            }
        };
    },
    computed: {
        ...mapState('user', ['userinfo'])

    },

    created() {
        // this.userForm = this.$store.state.user.userinfo
        // this.userForm = this.userinfo
        this.userForm = { ...this.userinfo }
    },

    methods: {
        resetForm() {
            // this.userForm = Object.assign({}, this.$store.state.userInfo);

            // 因为是值类型,所以使用浅拷贝,浅拷贝只拷贝第一层  拷贝值类型时, 拷贝 的就是值
            this.userForm = { ...this.userinfo }
            // this.userForm = this.$store.state.user.userinfo
        },
        // 兜底校验
        hSubmit() {
            // console.log('userinfo', this.userinfo)   //试一下能不能拿到vuex中的userinfo
            this.$refs.userFormRef.validate(valid => {
                if (!valid) console.log('兜底校验失败');
                //   修改
                this.doEdit()
            })
        },


        async doEdit() {
            try {
                const res = await eDitUserinfo(this.userForm)
                console.log('eDitUserinfo', res)
                //提示
                if (res.code !== 0) return this.$message.error(res.message)
                this.$message.success(res.message)
                //   更新vuex中的数据
                this.$store.dispatch('user/getUserinfo')

            } catch (err) {
                console.log('eDitUserinfo', err)
            }
        },
    }
}


</script>
  
<style lang="less" scoped>
.el-form {
    width: 500px;
}
</style>
  